<template>
  <div id="releaseBox">
    <!-- 头部 -->
    <van-nav-bar  fixed placeholder/>
    <div class="center">
      <van-form validate-first>
        <!-- 标题输入框 -->
        <!-- 通过 pattern 进行正则校验 -->
        <van-field
          class="input"
          v-model="title"
          name="title"
          placeholder="请输入标题"
        />
        <!-- 板块选择器 -->
        <div class="TitleBox">
          <van-field
            readonly
            clickable
            :value="cate_name"
            placeholder="请选择发布板块"
            @click="showPicker = true"
          />
          <van-popup v-model="showPicker" position="bottom">
            <van-picker
              show-toolbar
              :columns="columns"
              value-key="name"
              @confirm="onConfirm"
              @cancel="showPicker = false"
            />
          </van-popup>
        </div>
        <!-- 内容输入框 -->
        <van-field
          v-model="content"
          name="content"
          rows="5"
          autosize
          type="textarea"
          maxlength="6000"
          placeholder="请输入内容"
          show-word-limit
        />
        <!-- 上传图片 -->
        <div class="upImg">
          <van-field name="uploader">
            <template #input>
              <van-uploader v-model="fileList" />
            </template>
          </van-field>
        </div>


        <div class="RYesButton">
          <van-button type="primary" @click="release" block>发布</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { Uploader } from "vant";
import { addArticle } from "@/api/release.js";
import { getCateList } from "@/api/home.js";
import { mapState, mapActions } from "vuex";

export default {
  data() {
    return {
      title: "",
      content: "",
      fileList: [],
      columns: [],
      cate_id: "",
      cate_name: "",
      showPicker: false,
    };
  },
  components: {
    [Uploader.name]: Uploader,
  },
  created() {
    this.getCate();
  },
  computed: {
    ...mapState(["uid", "userInfo", "ifLongin"]),
  },
  methods: {
    ...mapActions(["Upload"]),
    async release() {
      let {
        title,
        content,
        fileList,
        uid,
        userInfo,
        cate_id,
        cate_name,
        ifLongin,
      } = this;
      let imageSrc = await this.Upload(fileList); //封装上传逻辑

      if (ifLongin) {
        addArticle({
          title,
          content,
          cate_name,
          cate_id,
          author_id: uid,
          imageSrc,
          author: userInfo.nickname,
        }).then((res) => {
          console.log(res);
          this.title = "";
          this.content = "";
          this.fileList = [];
          this.cate_name = "";
        });
      } else if (!ifLongin) {
        this.$toast("请登录后才能发布");
      }
    },
    getCate(m) {
      getCateList().then((res) => {
        this.columns = res.data;
      });
    },
    onConfirm({ name, _id }) {
      this.cate_name = name;
      this.cate_id = _id;
      this.showPicker = false;
    },
  },
};
</script>


<style lang="less" scoped>
#releaseBox {
  margin-bottom: 100px;
}
.van-cell{
  background-color: #f7f8fa;
} 
.input {
  margin-bottom: 10px;
}
.TitleBox {
  margin-bottom: 10px;
}
::v-deep .van-button--block {
  width: 70%;
}
::v-deep .van-button {
  background-color: #8b69a9;
  border: #8b69a9;
  margin-top: 10px;
  margin-left: 55px;
}
.upImg {
  .van-cell {
    background-color: #fff;
  }
}
</style>